<?php
namespace novel\controllers;

use common\models\NovelModel;
use common\models\NovelTypeModel;
use Yii;

use yii\data\Pagination; //分页
use novel\base\BaseController;
use yii\db\Expression;

/**
 * Novel controller
 */
class NovelController extends BaseController
{
    public $menu = "";
    public $defaultAction = 'index';

    public function beforeAction($action)
    {
        if (parent::beforeAction($action)) {
            $novel_type = parent::redisGet('novel_type',true);
            if(empty($index_novel)){
                $novel_type = NovelTypeModel::find()->select(['title', 'id', 'url'])->limit(16)->asArray()->all();
//                Yii::$app->cache->set('novel_type', $novel_type,24*60*60);
                parent::redisSet('novel_type', $novel_type);
            }
            return $this->menu = $novel_type;
        }
        return false;
    }

    /*
     * 清理缓存
     */
    public function actionClear()
    {
        Yii::$app->redis->flushall();
    }

    /*
     * 首页
     */
    public function actionIndex()
    {
        $index_novel = parent::redisGet('index_novel',true);
        if(empty($index_novel)) {
            $novel_type = parent::redisGet('novel_type',true);
            // $type_id = [555, 33, 333, 66, 888];
            // $type = [];
            // if (!empty($novel_type)) foreach ($novel_type as $k => $val) {
            //     if (in_array($val['id'], $type_id)) {
            //         $type[$val['id']] = $val['title'];
            //     }
            // }
            // $index_novel = NovelTypeModel::find()->select(['url', 'id'])->where(['in', 'id', $type_id])->asArray()->all();
            $index_novel = $novel_type;
            if (!empty($index_novel)) foreach ($index_novel as $k => $val) {
                // $index_novel[$k]['type_title'] = $type[$val['id']];
                $index_novel[$k]['type_title'] = $val['title'];
                // $type_id = explode(',', $val['url']);
                // $res = NovelModel::find()->select(['title', 'author'])->where(['in', 'type', $type_id])->limit(20)->with([
                $res = NovelModel::find()->select(['title', 'author'])->where(['type'=> $val['id']])->limit(20)->with([
                    'source' => function ($m) {
                        $m->select(['id','title', 'source', 'url']);
                    }
                ])->orderBy('click desc')->asArray()->all();
                $index_novel[$k]['list'] = $res;
            }
            // parent::redisSet('index_novel', $index_novel,864000);
            parent::redisSet('index_novel', $index_novel,864000);
        }
        self::setViewParams('华文小说,玄幻,奇幻,仙侠','华文小说,玄幻,奇幻,仙侠','华文小说,玄幻,奇幻,仙侠');
        return $this->render('index',['list'=>$index_novel,'source'=>Yii::$app->params['source']]);
    }


    /*
     * 分类 搜索列表
     * 来源 分类 关键字  点击次数排序
     */
    public function actionSearch()
    {
        $data = $this->body;
        $model = NovelModel::find();
        $search_name = '';
        if(isset($data['class_id'])){
            $novel_type = parent::redisGet($data['class_id'].'_type',true);
            if(empty($novel_type)) {
                $novel_type = NovelTypeModel::find()->select(['title', 'url'])->where(['id' => $data['class_id']])->asArray()->one();
                parent::redisSet($data['class_id'].'_type',$novel_type);
            }
            // if(strpos($novel_type['url'],',') !== false){
            //     $novel_type_ids = explode(',',$novel_type['url']);
            // }else{
            //     $novel_type_ids = $novel_type['url'];
            // }
            // $model = $model->andWhere(['in','type',$novel_type_ids]);
            $model = $model->andWhere(['type'=>$data['class_id']]);
            $search_name = $novel_type['title'];
        }
        if(isset($data['keyword'])){
            $model = $model->andWhere(['like','title',$data['keyword']]);
            $search_name = '关于-'.$data['keyword'].'-';
        }
        if(isset($data['source'])){
            $model = $model->andWhere(['source'=>$data['source']]);
            $search_name = Yii::$app->params['source'][$data['source']];
        }
        $count = $model->count();
        $page = 1;$limit = 40;
        if(isset($data['page'])){
            $page = $data['page'];$limit = !empty($data['limit']) ? $data['limit'] : $limit;
        }
        $model = $model->with([
            'source' => function ($m) {
                $m->select(['id','title', 'source', 'url']);
            }
        ])->offset(($page-1)*$limit)->limit($limit);
        if(isset($data['order'])){
            $model = $model->orderBy($data['order'].' desc');
        }else{
            $model = $model->orderBy('click desc');
        }
        $model = $model->asArray()->all();
        self::setViewParams($search_name,$search_name,$search_name);

        return $this->render('list',['list'=>$model,'search_name'=>$search_name, 'source'=>Yii::$app->params['source'],
            'page' => new Pagination(['totalCount' =>$count, 'pageSize' => $limit, 'pageSizeParam'=>false]),'count'=>$count]);
    }

    /*
     * 点击次数添加
     */
    public function actionClick()
    {
        $data = $this->body;
        $id = !empty($data['id']) ? $data['id'] : '';
        if($id){
            NovelModel::updateAll(
                ['click' => new Expression('`click` + 1')],
                ['id' => $id]);
        }
    }

}
